import { createStore  } from 'vuex'

const store = createStore({
    state: {
        user: null,
        token: null,
        // 货币相关状态
        currentCurrency: 'CNY', // 当前选择的货币
        exchangeRates: { // 汇率缓存
            CNY: 1.0, // 基准货币
            USD: 0.14,
            EUR: 0.13,
            JPY: 20.0
        },
        currencySymbols: { // 货币符号
            CNY: '¥',
            USD: '$',
            EUR: '€',
            JPY: '¥'
        },
        currencyNames: { // 货币名称
            CNY: '人民币',
            USD: '美元',
            EUR: '欧元',
            JPY: '日元'
        }
    },
    mutations: {
        setUser(state, user) {
            state.user = user
            localStorage.setItem('user', JSON.stringify(user))
        },
        setToken(state, token) {
            state.token = token
            localStorage.setItem('token', token)
        },
        // 货币相关 mutations
        setCurrentCurrency(state, currency) {
            state.currentCurrency = currency
            localStorage.setItem('currentCurrency', currency)
        },
        setExchangeRates(state, rates) {
            state.exchangeRates = { ...state.exchangeRates, ...rates }
            localStorage.setItem('exchangeRates', JSON.stringify(state.exchangeRates))
        },
        updateExchangeRate(state, { currency, rate }) {
            state.exchangeRates[currency] = rate
            localStorage.setItem('exchangeRates', JSON.stringify(state.exchangeRates))
        }
    },
    getters: {
        isLoggedIn(state) {
            state.user = JSON.parse(localStorage.getItem('user'))
            return state.user !== null
        },
        getToken(state) {
            state.token = localStorage.getItem('token')
            return state.token
        },
        getUser(state) {
            state.user = JSON.parse(localStorage.getItem('user'))
            return state.user
        },
        getUserId(state){
            state.user = JSON.parse(localStorage.getItem('user'));
            return state.user.userId;
        },
        // 货币相关 getters
        getCurrentCurrency(state) {
            const saved = localStorage.getItem('currentCurrency')
            return saved || state.currentCurrency
        },
        getCurrencySymbol(state) {
            const currency = state.currentCurrency || localStorage.getItem('currentCurrency') || 'CNY'
            return state.currencySymbols[currency] || '¥'
        },
        getCurrencyName(state) {
            const currency = state.currentCurrency || localStorage.getItem('currentCurrency') || 'CNY'
            return state.currencyNames[currency] || '人民币'
        },
        getExchangeRates(state) {
            const saved = localStorage.getItem('exchangeRates')
            return saved ? JSON.parse(saved) : state.exchangeRates
        },
        getExchangeRate: (state) => (currency) => {
            const rates = state.exchangeRates
            const saved = localStorage.getItem('exchangeRates')
            const exchangeRates = saved ? JSON.parse(saved) : rates
            return exchangeRates[currency] || 1.0
        }
    }
})

export default store